package com.jkoss.wine.inventory.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.inventory.entity.PackingSetting;
import com.jkoss.wine.inventory.service.IPackingSettingService;
import com.jkoss.wine.inventory.vo.PackingSettingVo;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.merchantOrder.vo.MerchantOrderVo;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.warehouse.entity.Warehouse;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 
 * 
 * @Author Supreme
 * @Version 1.0, 2018年9月30日
 * @See
 * @Since com.jkoss.wine.inventory.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/inventory/packingSetting")
public class PackingSettingController extends BaseController {

	@Autowired
	private IPackingSettingService iPackingSettingService;

	@Autowired
	private IMerchantProductNormService merchantIMerchantProductNormService;

	/**
	 * 列表
	 * 
	 * @param packingName
	 * @param packingNum
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/inventory/packingSetting/list")
	public String list(String packingName, String packingNum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string1 = request.getParameter("org1.packingName");
		String string2 = request.getParameter("org2.packingNum");
		dwzPageBean.getCountResultMap().put("packingName", string1);
		dwzPageBean.getCountResultMap().put("packingNum", string2);
		Wrapper wrapper = Condition.create().ne("del", 1).like("packing_name", string1).like("packing_num", string2);

		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}

		Page resultPage = iPackingSettingService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "inventory/packingSetting/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/inventory/packingSetting/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "inventory/packingSetting/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/inventory/packingSetting/toInsert")
	@ResponseBody
	public Object insert(@Valid PackingSetting packing_setting, HttpServletRequest request, HttpServletResponse response) throws Exception {

		/*
		 * Wrapper wrapperByPackingName = Condition.create().eq("packing_name",
		 * packing_setting.getPackingName()); Integer count =
		 * iPackingSettingService.selectCount(wrapperByPackingName); if (count > 0) {
		 * throw new Exception("名称已经存在"); }
		 */

		Wrapper wrapper = Condition.create().ne("del", 1).eq("packing_name", packing_setting.getPackingName());
		PackingSetting packingSettingExist = iPackingSettingService.selectOne(wrapper);
		if (!CommonUtil.isBlank(packingSettingExist)) {
			return ajaxError("货箱名已存在");
		}

		Wrapper wrapper2 = Condition.create().ne("del", 1).eq("packing_num", packing_setting.getPackingNum());
		PackingSetting packingSettingExist2 = iPackingSettingService.selectOne(wrapper2);
		if (!CommonUtil.isBlank(packingSettingExist2)) {
			return ajaxError("货箱编号已存在");
		}

		/*
		 * Wrapper wrapperByPackingNum = Condition.create().eq("packing_num",
		 * packing_setting.getPackingNum()); count =
		 * iPackingSettingService.selectCount(wrapperByPackingNum); if (count > 0) {
		 * throw new Exception("条形码记录已经存在"); }
		 */
		packing_setting.setMerchantProductNormId(request.getParameter("prdlookup.merchantProductNormId"));
		packing_setting.setMerchantProductId(request.getParameter("prdlookup.merchantProductId"));
		// 获取当前时间
		packing_setting.setAddTime(CommonUtil.getDate());
		// 获取当前操作人，当操作人为null时，表示自动录入
		packing_setting.setOperator(getUserId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		packing_setting.setDel(0);
		if (iPackingSettingService.insert(packing_setting)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/inventory/packingSetting/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		PackingSetting packingSetting = iPackingSettingService.selectById(id);
		map.put("record", packingSetting);
		Wrapper wrappermpn = Condition.create().eq("merchant_product_norm_id", packingSetting.getMerchantProductNormId());
		map.put("merchantProductNorm", merchantIMerchantProductNormService.selectOne(wrappermpn));
		return "inventory/packingSetting/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/inventory/packingSetting/toUpdate")
	@ResponseBody
	public Object update(@Valid PackingSetting packing_setting, HttpServletRequest request, HttpServletResponse response) {
		packing_setting.setMerchantProductNormId(request.getParameter("prdlookup.merchantProductNormId"));
		packing_setting.setMerchantProductId(request.getParameter("prdlookup.merchantProductId"));

		Wrapper wrapper2 = Condition.create().ne("del", 1).eq("packing_num", packing_setting.getPackingNum()).ne("packing_setting_id", packing_setting.getPackingSettingId());
		PackingSetting packingSettingExist2 = iPackingSettingService.selectOne(wrapper2);
		if (!CommonUtil.isBlank(packingSettingExist2)) {
			return ajaxError("货箱编号已存在");
		}

		if (iPackingSettingService.updateById(packing_setting)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/inventory/packingSetting/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			PackingSetting packing_setting = new PackingSetting();
			packing_setting.setPackingSettingId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			packing_setting.setDel(1);
			list.add(packing_setting);
		}
		if (iPackingSettingService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询商品名/商品编码
	 * 
	 * @param normName
	 * @param productNum
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/chooseProductList")
	public String listMerchantProduct(String normName, String productNum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("normName", normName);
		dwzPageBean.getCountResultMap().put("productNum", productNum);
		Wrapper wrapper = Condition.create().like("norm_name", normName).like("product_num", productNum).ne("del", 1);
		Page resultPage = merchantIMerchantProductNormService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "inventory/packingSetting/chooseProductList";
	}

	/**
	 * 查询货箱名
	 * 
	 * @param packingName
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getPackingName")
	@ResponseBody
	public Object getShopName(String packingName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("packing_name", packingName).ne("del", 1);
		List list = iPackingSettingService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 查询货箱编码
	 * 
	 * @param packingNum
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getPackingNum")
	@ResponseBody
	public Object getPackingNum(String packingNum, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("packing_num", packingNum).ne("del", 1);
		List list = iPackingSettingService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/inventory/packingSetting/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iPackingSettingService.selectPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "货箱列表", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, PackingSetting.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "货箱列表");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

}
